<?php

$valid_formats = array (
		"jpg",
		"png",
		"gif",
		"bmp" 
);
if (isset ( $_POST ) and $_SERVER ['REQUEST_METHOD'] == "POST") {
	$name = $_FILES ['photoimg'] ['name'];
	$size = $_FILES ['photoimg'] ['size'];
	
	if (strlen ( $name )) {
		list ( $txt, $ext ) = explode ( ".", $name );
		if (in_array ( $ext, $valid_formats )) {
			if ($size < (1024 * 1024)) {
				
				// Sleep
				sleep(1);
				
				
				$width = 160;
				$height = 160;
				$image = imagecreatefromjpeg($_FILES['photoimg']['tmp_name']);
				$orig_width = imagesx($image);
				$orig_height = imagesy($image);
				
				// Create new image to display
				$new_image = imagecreatetruecolor($width, $height);
				
				// Create new image with change dimensions
				imagecopyresized($new_image, $image,
				0, 0, 0, 0,
				$width, $height,
				$orig_width, $orig_height);
				
				// Print image
				ob_start();
				imagejpeg($new_image);
				$data = ob_get_contents();
				ob_end_clean();
				$url_Image = '{ "url_Image": "data:image/jpeg;base64,' . base64_encode($data) . '" }';
				echo $url_Image;
				//echo "<img src='data:image/jpeg;base64," . base64_encode($data) . "' class='preview'>";
				
			} else
				echo "Image file size max 1 MB";
		} else
			echo "Invalid file format..";
	} 

	else
		echo "Please select image..!";
	
	exit ();
}
?>